package com.xmu.edu.tisp.service.basedata;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.xmu.edu.tisp.dao.Service;
import com.xmu.edu.tisp.dao.TISP_dbHelper;

public class PDA_PARAM_service implements Service{
	

	public static final String TABLE_NAME = "PDA_PARAM";
	public static final String PRIMARY_KEY="PDA_PARAM_ID";
	
	private SQLiteDatabase myDB;
	private SQLiteOpenHelper dbHelper;


	public PDA_PARAM_service(Context cxt) {
		dbHelper=new TISP_dbHelper(cxt, "TISP");
	}

	public SQLiteDatabase getdb()
	{
		if(myDB==null)
		{
			myDB=dbHelper.getWritableDatabase();
			return myDB;
		}
		return myDB;
	}
	
	@Override
	public void g_updata(ContentValues values) {
		getdb().update(TABLE_NAME, values, PRIMARY_KEY+"=?", new String[]{(String) values.get(PRIMARY_KEY)});
		System.out.println(TABLE_NAME+"have updataed!");
		
	}

	@Override
	public void g_insert(ContentValues values) {
		getdb().insert(TABLE_NAME, null, values);
		
		System.out.println(TABLE_NAME+"have inserted!");
	}

	@Override
	public void g_delete(String[] id) {
		getdb().delete(TABLE_NAME, PRIMARY_KEY+"=?", id);
		System.out.println(TABLE_NAME+"have deleted!");
		
	}


	@Override
	public List<String> g_getIDs() {
		Cursor c=getdb().query(TABLE_NAME, new String[]{PRIMARY_KEY},null, null, null, null, null);
		List<String> result=new ArrayList<String>();
		while(c.moveToNext())
		{
			String id=c.getString(c.getColumnIndex(PRIMARY_KEY));
			result.add(id);
		}
		return result;
	}
	
	public Cursor fetchAllData(){
		Cursor cursor=getdb().query(TABLE_NAME, new String[] { "PDA_PARAM_ID", 
                 
                "PARAM_CODE", 
                "PARAM_VALUE" }, 
                null, null, null, null, null);

        cursor.deactivate();
		return cursor;
		
	}
	
	//-----------------------------------------
	public void close()
	{
		getdb().close();
		System.out.println("db closed");
	}
	
	//String 
	public String getIsCalendar()
	{
		String iscal="";
		String sql;
	
		sql="select param_value as a from "+TABLE_NAME+" where param_code is 'PRECHECKREFIRE'; ";  //8a0ddd9a28b47f370128b4d5c9430001
		Cursor cursor = getdb().rawQuery(sql, null);
		while(cursor.moveToNext()) {
			iscal=(cursor.getString(cursor.getColumnIndex("a")));	
		}	
System.out.println("pda_param_service  104 @@@@@@@@@@@@@@@@@@@@@@@@@"+iscal);
		cursor.close();
		return iscal;
	}

	//v <---  nothing
	public Vector getTimeSlotsToBeSelected()
	{
		Vector s=new Vector<String>();
		String sql;
		sql="select param_value as a from pda_param where param_code='" +
				"PRECHECKDATE';";
		
		Cursor cursor = getdb().rawQuery(sql, null);
		while(cursor.moveToNext()) {
			s.add(cursor.getString(cursor.getColumnIndex("a")));	
		}		
		cursor.close();
		return  s;
		
	}

}
